package com.ys.conf.exception;

import com.ys.enu.ResultCodeEnum;
import com.ys.exception.base.BizException;
import com.ys.exception.base.ParamException;
import com.ys.web.base.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理类
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionConf {


    @ExceptionHandler(value=Exception.class)
    public ResultBody defaultErrorHandler(HttpServletRequest req, Exception e){
        log.error("服务器调用错误：地址"+req.getRequestURI(),e);
        return new ResultBody(ResultCodeEnum.fail);
    }


    @ExceptionHandler(value=BizException.class)
    public ResultBody BizExceptionErrorHandler(HttpServletRequest req, BizException e){
        log.error("服务器调用错误：地址"+req.getRequestURI(),e);
        return new ResultBody(e.getCode(),e.getMsg());
    }
    @ExceptionHandler(value=ParamException.class)
    public ResultBody ParamExceptionErrorHandler(HttpServletRequest req, ParamException e){
        log.error("服务器调用错误：地址"+req.getRequestURI(),e);
        return new ResultBody(e.getCode(),e.getMsg());
    }


}
